Trò chơi đua xe động vật trong UNITY Engine
114.689 lượt xem;
- Animation.cs
- AnimalRacing /
- Scripts /
- Assets /
- project /
1 using UnityEngine;
2 using System.Collections;
3
4 namespace Bapk
5 {
6
7 public class Animation : MonoBehaviour
8 {
9 public Sprite[] sprites;
10 public float durationOneFrame;
11 public bool repeat;
12 public bool destroyWhenFinish;
13
14 private int frame;
15 private float stateTime;
16 private int currentIndex;
17 private bool isFinish;
18
19 private bool isRunning;
20
21 public void Start()
22 {
23 currentIndex = 0;
24 gameObject.GetComponent<SpriteRenderer>().sprite = sprites[currentIndex];
25 frame = sprites.Length;
26 isRunning = true;
27 }
28
29 public void Update()
30 {
31 if (isRunning)
32 {
33 UpdateAnimation();
34 }
35 }
36
37 private void UpdateAnimation()
38 {
39 stateTime += Time.deltaTime;
40 if (stateTime >= durationOneFrame)
41 {
42 if (isFinish)
43 {
44 if (destroyWhenFinish)
45 {
46 Destroy(gameObject);
47 return;
48 }
49 }
50 currentIndex++;
51 if (currentIndex == frame - 1)
52 {
53 if (!repeat)
54 isFinish = true;
55 }
56 if (currentIndex == frame)
57 currentIndex = 0;
58 stateTime = 0;
59 gameObject.GetComponent<SpriteRenderer>().sprite = sprites[currentIndex];
60 }
61 }
62
63 public void setRunning(bool isRunning)
64 {
65 this.isRunning = isRunning;
66 }
67 }
68 }
2 using System.Collections;
3
4 namespace Bapk
5 {
6
7 public class Animation : MonoBehaviour
8 {
9 public Sprite[] sprites;
10 public float durationOneFrame;
11 public bool repeat;
12 public bool destroyWhenFinish;
13
14 private int frame;
15 private float stateTime;
16 private int currentIndex;
17 private bool isFinish;
18
19 private bool isRunning;
20
21 public void Start()
22 {
23 currentIndex = 0;
24 gameObject.GetComponent<SpriteRenderer>().sprite = sprites[currentIndex];
25 frame = sprites.Length;
26 isRunning = true;
27 }
28
29 public void Update()
30 {
31 if (isRunning)
32 {
33 UpdateAnimation();
34 }
35 }
36
37 private void UpdateAnimation()
38 {
39 stateTime += Time.deltaTime;
40 if (stateTime >= durationOneFrame)
41 {
42 if (isFinish)
43 {
44 if (destroyWhenFinish)
45 {
46 Destroy(gameObject);
47 return;
48 }
49 }
50 currentIndex++;
51 if (currentIndex == frame - 1)
52 {
53 if (!repeat)
54 isFinish = true;
55 }
56 if (currentIndex == frame)
57 currentIndex = 0;
58 stateTime = 0;
59 gameObject.GetComponent<SpriteRenderer>().sprite = sprites[currentIndex];
60 }
61 }
62
63 public void setRunning(bool isRunning)
64 {
65 this.isRunning = isRunning;
66 }
67 }
68 }